package com.geeaks.learn.concurrent.sermaphore;

import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.ReentrantLock;

public class ResourceManage {
	
	private final Semaphore semaphore;
	
	private boolean resourceArray[];
	
	private final ReentrantLock lock;
	
	public ResourceManage() {
		this.resourceArray = new boolean[3];// 存放厕所状态
		this.semaphore = new Semaphore(3, true);// 控制3个共享资源的使用，使用先进先出的公平模式进行共享;公平模式的信号量，先来的先获得信号量
		this.lock = new ReentrantLock(true);// 公平模式的锁，先来的先选
		for (int i = 0; i < 3; i++) {
			resourceArray[i] = true;// 初始化为资源可用的情况
		}
	}
	
	public void useResource(int userId) throws InterruptedException {
		semaphore.acquire();
		try {
			// semaphore.acquire();
			int id = getResourceId();// 占到一个坑
			System.out.println(userId + ":正在使用资源，资源id:" + id);
			Thread.sleep(100);// do something，相当于于使用资源
			resourceArray[id] = true;// 退出这个坑
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			semaphore.release();// 释放信号量，计数器加1
		}
	}
	
	private int getResourceId() {
		int id = -1;
		lock.lock();
		try {
			for (int i = 0; i < 3; i++) {
				if (resourceArray[i]) {
					resourceArray[i] = false;
					id = i;
					break;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
		return id;
	}
}
